//
//  ViewController.m
//  CoreData_持久化存储
//
//  Created by 何欣 on 16/3/15.
//  Copyright © 2016年 蓝鸥科技. All rights reserved.
//

#import "ViewController.h"

#import "CoreDataManager.h"
#import "Person.h"



@interface ViewController ()

///搜索后的数据
@property (nonatomic,retain)NSMutableArray *dataSource;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
}



- (IBAction)add:(id)sender {
    
    //获取实体描述
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:[CoreDataManager shareManager].managedObjectContext];
    Person *person = [[Person alloc]initWithEntity:entity insertIntoManagedObjectContext:[CoreDataManager shareManager].managedObjectContext];
    
    //另一种创建方式
    Person *aPerson = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:[CoreDataManager shareManager].managedObjectContext];
    
   
    person.name = @"王爵";
    person.age  = @18;
    person.sex  = @"不详";
    
    person.hobby = @"帅博洗澡";
    
    //保存
    [[CoreDataManager shareManager]saveContext];
    
    NSLog(@"路径 ：%@",[[CoreDataManager shareManager] applicationDocumentsDirectory]);
    
    NSLog(@"增");
    
}

- (IBAction)delete:(id)sender {
    
    Person *person = _dataSource.firstObject;
    [[CoreDataManager shareManager].managedObjectContext deleteObject:person];
    
    [[CoreDataManager shareManager]saveContext];
    
    
    NSLog(@"删");

}

- (IBAction)upData:(id)sender {
    
    Person *person = _dataSource.lastObject;
    person.name = @"帅博";
    person.age  = @3;
    person.sex  = @"女";
    
    [[CoreDataManager shareManager]saveContext];
    NSLog(@"改");

}

- (IBAction)select:(id)sender {
    
    //创建搜索请求
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:[CoreDataManager shareManager].managedObjectContext];
    //设置搜索的实体
    [fetchRequest setEntity:entity];
    // Specify criteria for filtering which objects to fetch
    //谓词 设置搜索条件

//    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name = %@",@"王爵"];
//    [fetchRequest setPredicate:predicate];
    // Specify how the fetched objects should be sorted
    //以某个字段排序  第二个参数  是否升序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"age"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error = nil;
    //搜索返回数据
    NSArray *fetchedObjects = [[CoreDataManager shareManager].managedObjectContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"error : %@",error);
    }
    
    self.dataSource = [NSMutableArray arrayWithArray:fetchedObjects];
    
    for (Person *person in fetchedObjects) {
        
        NSLog(@"名字 : %@ ",person.hobby);
        
    }
    
    
    NSLog(@"个数 : %ld",fetchedObjects.count);
    
    NSLog(@"查");

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
